//
//  CenterVC.m
//  mentor
//
//  Created by 孙宏峰的MacPro on 2018/7/4.
//  Copyright © 2018年 孙宏峰的MacPro. All rights reserved.
//

#import "CenterVC.h"
#import "LoginVC.h"
#import "DetaileVC.h"
#import "TouristsViewController.h"
#import "ShowDocVC.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface CenterVC ()<UIWebViewDelegate,UIDocumentPickerDelegate>
@property (strong, nonatomic) UIWebView *webView;
@property (nonatomic,copy)NSString *userType;
@property (nonatomic,strong) JSContext *context;

@end

@implementation CenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.view addSubview:self.webView];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString * urlStr = @"";
    self.userType = [CurrentUser shared].userType;
    if([[CurrentUser shared].userType isEqualToString:@"manager"]){  /*!@brief ****   小秘端  学生列表 */
        urlStr = [NSString stringWithFormat:@"%@%@.html",HTML_HOST_HEADER,HTML_ACTION_MSSTUDENTLIST];
    }else if ([[CurrentUser shared].userType isEqualToString:@"mentor"]){  /*!@brief ****   导师端 导师首页 */
        urlStr = [NSString stringWithFormat:@"%@%@.html",HTML_HOST_HEADER,HTML_ACTION_MENTORHOME];
    }else {  /*!@brief ****  家长端   家长端首页  */
        urlStr = [NSString stringWithFormat:@"%@%@.html",HTML_HOST_HEADER,HTML_ACTION_PARENT];
    }
    urlStr = [NSString stringWithFormat:@"%@?token=%@",urlStr,[CurrentUser shared].token];
    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];//ios 9以后
    NSURL *url = [NSURL URLWithString:urlStr];
    
    //TODO::测试
    NSURLRequest * request = [NSURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:30];
    
    NSLog(@"request:%@",request);
    [self.webView loadRequest:request];
}
#pragma mark - life cycle
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"打印：%@",request.URL.absoluteString);
    NSString *absoluteString = request.URL.absoluteString;
    
    if([self.userType isEqualToString:@"manager"]) {
        if ([absoluteString rangeOfString:HTML_ACTION_STUDENTINFO].location !=NSNotFound ||
            [absoluteString rangeOfString:HTML_ACTION_MSMENTORINFOEDIT].location != NSNotFound ||
            [absoluteString rangeOfString:HTML_ACTION_MSSTUDENTINFOEDIT].location != NSNotFound
            ) {
            DetaileVC * detailVC = [[DetaileVC alloc]init];
            detailVC.urlStr = absoluteString;
            detailVC.navTitle = [NSString noNull:[[absoluteString getURLParameters:absoluteString]objectForKey:@"title"]];
            [self.navigationController pushViewController:detailVC animated:YES];
            return NO;
        }
    }
    if([self.userType isEqualToString:@"mentor"]) {
        if([absoluteString rangeOfString:HTML_ACTION_STUDENTINFO].location != NSNotFound) {
            DetaileVC * detailVC = [[DetaileVC alloc]init];
            detailVC.urlStr = absoluteString;
            detailVC.navTitle = [NSString noNull:[[absoluteString getURLParameters:absoluteString]objectForKey:@"title"]];
            [self.navigationController pushViewController:detailVC animated:YES];
            return NO;
        }
    }
    if([self.userType isEqualToString:@"parent"]) {
        if([absoluteString rangeOfString:HTML_ACTION_MENTORDETAIL].location != NSNotFound || [absoluteString rangeOfString:HTML_ACTION_ZHIDAOINFO].location != NSNotFound) {
            DetaileVC * detailVC = [[DetaileVC alloc]init];
            detailVC.urlStr = absoluteString;
            detailVC.navTitle = [NSString noNull:[[absoluteString getURLParameters:absoluteString]objectForKey:@"title"]];
            [self.navigationController pushViewController:detailVC animated:YES];
            return NO;
        }
    }
    return YES;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //接受JC的调用
    //首先创建JSContext 对象（此处通过当前webView的键获取到jscontext）
    self.context=[webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    WS(weakSelf);
    
    self.context[@"openFileView"] =^(NSString *url)
    {
        NSArray *args = [JSContext currentArguments];
        JSValue *value = args[0];
        dispatch_async(dispatch_get_main_queue(), ^{
            ShowDocVC *docVC = [[ShowDocVC alloc]init];
            docVC.url = [NSString stringWithFormat:@"%@",value];
            [weakSelf.navigationController pushViewController:docVC animated:YES];
            
        });
    };
}
#pragma mark - customDelegate
#pragma mark - event Response
#pragma mark - private methods
- (void)userCenter
{
}
#pragma mark - getters and setters
- (UIWebView *)webView
{
    if (!_webView ) {
        _webView = [[UIWebView alloc] initWithFrame:(CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64))];
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
    }
    return _webView;
}
- (void)zhux
{
    TouristsViewController *tourVC = [[TouristsViewController alloc]init];
    BaseNav *nav = [[BaseNav alloc]initWithRootViewController:tourVC];
    
    [self presentViewController:nav animated:YES completion:^{
        [self.tabBarController setSelectedIndex:0];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
